#! usr/bin/python


"""
Copyright (c): Marc Dubois                                                
contributors: Pierre Millard, Fabien Jourdan, Ludovic Cottret         
Fabien Letisse                                                        

m_dubois@insa-toulouse.fr                                             

This software is a computer program whose purpose is to treat data    
from Mass Spectrometry.                                               

This software is governed by the CeCILL license under French law and  
abiding by the rules of distribution of free software.  You can  use, 
modify and/ or redistribute the software under the terms of the CeCILL
license as circulated by CEA, CNRS and INRIA at the following URL     
"http://www.cecill.info".                                             

As a counterpart to the access to the source code and  rights to copy,
modify and redistribute granted by the license, users are provided only
with a limited warranty  and the software's author,  the holder of the
economic rights,  and the successive licensors  have only  limited    
liability.                                                            

In this respect, the user's attention is drawn to the risks associated
with loading,  using,  modifying and/or developing or reproducing the
software by the user in light of its specific status of free software,
that may mean  that it is complicated to manipulate,  and  that  also
therefore means  that it is reserved for developers  and  experienced
professionals having in-depth computer knowledge. Users are therefore
encouraged to load and test the software's suitability as regards their
requirements in conditions enabling the security of their systems and/or 
data to be ensured and,  more generally, to use and operate it in the 
same conditions as regards security. 

The fact that you are presently reading this means that you have had
knowledge of the CeCILL license and that you accept its terms.
"""


from PyQt4 import QtCore, QtGui
from gui import visu_gui, integr_gui, cluster_gui, identif_gui, convert_gui, canvas_GL, canvas_spectrogram
import visu_control, integr_control, cluster_control, identif_control, web_browser_control
import sys
import matplotlib as m
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np
import os.path as path
from controller import BaseController as base


class Menu_Control(base):
	"""
	class handling the control of the menu bar
	"""
	
	def __init__(self, win, lspl):
		"""
		constructor
		connection definitions
		"""
		base.__init__(self, lspl, win)
		
		#file menu
		QtCore.QObject.connect(self.main_window.lst_file_actions[0], QtCore.SIGNAL("triggered()"),self.OpenFile)
		QtCore.QObject.connect(self.main_window.lst_file_actions[1], QtCore.SIGNAL("triggered()"),self.MergeFile)
		QtCore.QObject.connect(self.main_window.lst_file_actions[3], QtCore.SIGNAL("triggered()"),self.Convert)
		
		#algo menu
		QtCore.QObject.connect(self.main_window.lst_algo_actions[0], QtCore.SIGNAL("triggered()"),self.Parameters)
		QtCore.QObject.connect(self.main_window.lst_algo_actions[1], QtCore.SIGNAL("triggered()"),self.Clustering)
		QtCore.QObject.connect(self.main_window.lst_algo_actions[3], QtCore.SIGNAL("triggered()"),self.Baseline)
		
		#id menu
		QtCore.QObject.connect(self.main_window.lst_id_actions[0], QtCore.SIGNAL("triggered()"),self.Identification)
		
		#view menu
		QtCore.QObject.connect(self.main_window.lst_view_actions[0], QtCore.SIGNAL("triggered()"),self.show_3D)
		QtCore.QObject.connect(self.main_window.lst_view_actions[1], QtCore.SIGNAL("triggered()"),self.show_cytoscape)
		QtCore.QObject.connect(self.main_window.lst_view_actions[2], QtCore.SIGNAL("triggered()"),self.show_spectrogram)
		
		#tools menu
		QtCore.QObject.connect(self.main_window.lst_tools_actions[0], QtCore.SIGNAL("triggered()"),self.launchWeb)
		QtCore.QObject.connect(self.main_window.lst_view_actions[1], QtCore.SIGNAL("triggered()"),self.show_cytoscape)

		#about
		QtCore.QObject.connect(self.main_window.lst_about_actions[0], QtCore.SIGNAL("triggered()"),self.showQt)
		QtCore.QObject.connect(self.main_window.lst_about_actions[1], QtCore.SIGNAL("triggered()"),self.showDoc)
	
	
	
	def show_cytoscape(self):
		"""
		launch cytoscape with the specified file, sample must have peaks
		"""
		if not len(self.lspl):
			self.main_window.ShowErrorMessage("Error", "No sample parsed, please open one file")
			return
		string, boolean = QtGui.QInputDialog.getItem(self.main_window,"Graph Visualization", "Choose one sample :", [spl.getXmlFile() for spl in self.lspl], \
						0, False)
		if not self.lspl.Sample(string).get_peak_list():
			self.main_window.ShowErrorMessage("Error", "No peaks found in "+ str(string))
			return
		web = web_browser_control.httpWidget(self.main_window)
		web.ui.tab_widget.currentWidget().webview.load(QtCore.QUrl(path.normcase("controller/test.html")))
		self.main_window.addMdiSubWindow(web)

	
	def show_spectrogram(self):
		"""
		provide scatterplot visualisation of the sample
		"""
		if not len(self.lspl):
			self.main_window.ShowErrorMessage("Error", "No sample parsed, please open one file")
			return
		string, boolean = QtGui.QInputDialog.getItem(self.main_window,"Spectrum", "Choose one sample :", [spl.getXmlFile() for spl in self.lspl], \
						0, False)
		if boolean and string:
			self.main_window.ShowInStatusBar("Plotting points.Please Wait...", 10000)
			self.main_window.addMdiSubWindow(canvas_spectrogram.Spectrogram(self.lspl.Sample(string)).get_canvas(), "Spectrogram of "+ string)

	
	def show_3D(self):
		"""
		provide 3D view of all the sample
		"""
		if not len(self.lspl):
			self.main_window.ShowErrorMessage("Error", "No sample parsed, please open one file")
			return
		string, boolean = QtGui.QInputDialog.getItem(self.main_window,"3D plot", "Choose one sample :", [spl.getXmlFile() for spl in self.lspl], \
						0, False)
		if boolean and string:
			self.main_window.ShowInStatusBar("3D plotting.Please Wait...", 10000)
			wid =canvas_GL.canvas_GL(self.lspl.Sample(string))#
			self.main_window.addMdiSubWindow(wid, " ".join(["3D plotting of", str(string)]))
			
	
	"""
	DEPRECATED
	def show_correlation(self):
		if not len(self.lspl):
			self.main_window.ShowErrorMessage("Error", "No sample parsed, please open one file")
			return
		string, boolean = QtGui.QInputDialog.getItem(self.main_window,"3D plot", "Choose one sample :", [spl.getXmlFile() for spl in self.lspl], \
						0, False)
		if not any ([len(peak.get_frag_cluster()) for peak in self.lspl.Sample(string).get_peak_list()]):
			self.main_window.ShowErrorMessage("Error", "No cluster found in this file "+string)
			return
	"""
	
	def Baseline(self):
		string, boolean = QtGui.QInputDialog.getItem(self.main_window,"Spectrum", "Choose one sample :", [spl.getXmlFile() for spl in self.lspl], \
						0, False)
		if boolean and string:
			for chroma in spl._gen_chromas():pass
	
	def showQt(self):
		"""
		show qt information
		"""
		QtGui.QMessageBox.aboutQt(self.main_window, "About")
	
	def showDoc(self):
		"""
		API pySpec
		"""
		web = web_browser_control.httpWidget(self.main_window)
		web.ui.tab_widget.currentWidget().webview.load(QtCore.QUrl(path.normcase("doc/html/index.html")))
		self.main_window.addMdiSubWindow(web)
		
	def launchWeb(self):
		"""
		launch Webyo
		"""
		web = web_browser_control.httpWidget(self.main_window)
		self.main_window.addMdiSubWindow(web)
	
	def OpenFile(self):
		"""
		Dialog for opening file
		"""
		visu = visu_gui.VisuWidget(parent = self.main_window)
		control = visu_control.visualisation_controller(self.lspl, visu)
		visu.exec_()
	
	def MergeFile(self):
		"""
		dialog for merging files
		"""
		self.main_window.ShowInformationMessage("Information",\
		"We strongly recommand you to merge files during the peak-picking")
		visu = visu_gui.VisuWidget(parent = self.main_window)
		control = visu_control.visualisation_controller(self.lspl, visu)
		visu.exec_()
	
	def Convert(self):
		""" 
		dialog for conversion
		"""
		convert =convert_gui.convertWidget(parent = self.main_window)
		convert.exec_()
	
	def Parameters(self):
		"""
		dialog for integration
		"""
		integr = integr_gui.IntegrWidget(parent = self.main_window)
		control = integr_control.integration_controller(self.lspl, integr)
		integr.exec_()
		
	def Clustering(self):
		"""
		dialog for clustering step
		"""
		clust = cluster_gui.clusterWidget(parent =self.main_window)
		control = cluster_control.cluster_controller(self.lspl, clust)
		clust.exec_()
	
	def Identification(self):
		"""
		dialog for identification step
		"""
		identif = identif_gui.identifWidget(parent =self.main_window)
		control =identif_control.identif_control(self.lspl, identif)
		identif.exec_()
		
	
	def Quit(self):
		"""
		close the event loop and so the application
		"""
		QtGui.QApplication.quit()
		
